Merged
Conversation
6c9c6dd to
a94f570
Compare
a94f570 to
fefc780
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes the handling of optional request bodies in the OpenAPI Python client generator. Previously, optional bodies were incorrectly treated as required, causing them to always be included in requests. The fix ensures that optional bodies (where requestBody.required is false or missing) are properly typed with Unset, default to UNSET, and are only included in requests when explicitly provided.
Key Changes:
- Modified body parsing to respect the
requiredfield from request body definitions - Updated all property transform templates to support a
skip_unsetparameter for conditional UNSET initialization - Added deduplication of union types to prevent duplicate type members
- Generated code now properly handles optional bodies across different content types (JSON, multipart, octet-stream, etc.)
Reviewed changes
Copilot reviewed 32 out of 32 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| openapi_python_client/parser/bodies.py | Passes the actual body.required value instead of hardcoding True |
| openapi_python_client/parser/properties/union.py | Adds deduplication logic to remove duplicate types from unions and uses Iterator for better memory efficiency |
| openapi_python_client/templates/property_templates/*.py.jinja | Adds skip_unset parameter to transform macros to conditionally skip UNSET initialization |
| openapi_python_client/templates/endpoint_macros.py.jinja | Updates body handling to check if bodies are optional and conditionally include them in requests |
| openapi_python_client/templates/endpoint_module.py.jinja | Uses no_optional=True for isinstance checks with multiple bodies |
| end_to_end_tests/golden-record/* | Updated golden records showing correct handling of optional bodies with Unset types |
| end_to_end_tests/baseline_openapi_. | Added test cases for optional bodies and duplicate union types |
| tests/test_parser/test_properties/test_union.py | Updated test to verify different property types in unions |
| .changeset/fix_optional_bodies.md | Documents the fix for optional bodies |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
openapi_python_client/templates/property_templates/file_property.py.jinja
Show resolved
Hide resolved
openapi_python_client/templates/property_templates/enum_property.py.jinja
Show resolved
Hide resolved
openapi_python_client/templates/property_templates/literal_enum_property.py.jinja
Show resolved
Hide resolved
openapi_python_client/templates/property_templates/list_property.py.jinja
Show resolved
Hide resolved
end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/bodies/__init__.py
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reopening #1357